import Vue from 'vue';
import Vuex from 'vuex';
import Cookie from 'js-cookie'

Vue.use(Vuex);

const state = () => ({
  item22222222Title: 'DetailPage',
  item2222222222222Date: '2021-10-31',
});

const mutations = {
  SET_TOKEN: (state, token) => {
    state.token = token
    Cookie.set('token', token)
  },
  SET_NAME: (state, name) => {
    state.name = name
    Cookie.set('name', name)
  },
  SET_HEADIMG: (state, img) => {
    state.headImg = img
    Cookie.set('headImg', img)
  }
};

// 存在异步操作使用action，直接调用mutation中的函数
const actions = {
  modifyDate(context,payload) {
    context.commit('modifyDate',payload)
  },
  async nuxtServerInit({ commit }, { app }) {
    commit('user/SET_TOKEN', app.$cookies.get("token"))
    commit('user/SET_INFO', app.$cookies.get("info"))
  }
}

export default {
  state,
  mutations,
  actions,
}